
#include <gfx/renderenvironment.h>
#include <scene/lodnode.h>
#include <scene/sector.h>

ceLODNode::ceLODNode()
  : ceNode ()
  , _node (0)
{
}

ceLevelOfDetail<ceNode>& ceLODNode::Get()
{
  return _nodes;
}

const ceLevelOfDetail<ceNode>& ceLODNode::Get() const
{
  return _nodes;
}

ceNode* ceLODNode::GetNode()
{
  return _node;
}

const ceNode* ceLODNode::GetNode() const
{
  return _node;
}

ceNodeType ceLODNode::GetNodeType() const
{
  return NT_LODNode;
}

void ceLODNode::Update(ceRenderEnvironment &env, iDevice *)
{
  float dist = (env.CameraLocationGlobal - GetBoundingBox().GetCenter()).Length();
  _node = _nodes.Eval(dist, GetBoundingBox().GetMediumRadius());
}

void ceLODNode::UpdateTransformation()
{
  for (unsigned i=0, ni=_nodes.GetNumberOfLevels(); i<ni; ++i)
    {
      ceNode* node = _nodes.Get(i);
      node->FinishTransformation(false);
    }
}

void ceLODNode::FillPrivateBoundingBox(ceBoundingBox &bbox)
{
  for (unsigned i=0, ni=_nodes.GetNumberOfLevels(); i<ni; ++i)
    {
      const ceNode* n = _nodes.Get(i);
      bbox.Add(n->GetBoundingBox());
    }
}


void ceLODNode::FindLights (ceSector* sector)
{
  for (unsigned i=0, j=_nodes.GetNumberOfLevels(); i<j; ++i)
    {
      ceNode* n = _nodes.Get(i);
			n->FindLights (sector);
    }
}

void ceLODNode::ClearLights ()
{
  for (unsigned i=0, j=_nodes.GetNumberOfLevels(); i<j; ++i)
    {
      ceNode* n = _nodes.Get(i);
			n->ClearLights ();
    }
}

void ceLODNode::TestAndAddLight (ceLight* light)
{
  for (unsigned i=0, j=_nodes.GetNumberOfLevels(); i<j; ++i)
    {
      ceNode* n = _nodes.Get(i);
			n->TestAndAddLight (light);
    }
}
